package com.tim.javaApi.reflect;


import org.springframework.beans.BeanUtils;

import java.beans.*;
import java.util.Arrays;
import java.util.Date;

/**
 * @Description: java、spring bean操作api
 * @Version 1.0, 2021/9/25
 * @Author Tim
 */
public class BeanHandleUtil2 {
    
    public void introspector() throws IntrospectionException {
        BeanInfo beanInfo = Introspector.getBeanInfo(Person.Teacher.class);
        System.out.println("propertyDescriptors");
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        Arrays.stream(propertyDescriptors)
                .forEach(System.out::println);
        System.out.println("methodDescriptors");
        MethodDescriptor[] methodDescriptors = beanInfo.getMethodDescriptors();
        Arrays.stream(methodDescriptors)
                .forEach(System.out::println);


        System.out.println("eventSetDescriptors");
        EventSetDescriptor[] eventSetDescriptors = beanInfo.getEventSetDescriptors();
        Arrays.stream(eventSetDescriptors)
                .forEach(System.out::println);
        System.out.println("additionalBeanInfo");
        BeanInfo[] additionalBeanInfo = beanInfo.getAdditionalBeanInfo();
        Arrays.stream(additionalBeanInfo)
                .forEach(System.out::println);
        System.out.println("getBeanDescriptor");
        System.out.println(beanInfo.getBeanDescriptor());

    }

    // getter、setter操作
    
    public void propertyDescriptor() throws Exception {
        PropertyDescriptor propertyDescriptor = new PropertyDescriptor("age", Person.Teacher.class);
        Person.Teacher person = new Person.Teacher();
        System.out.println(propertyDescriptor.getReadMethod().invoke(person));
        propertyDescriptor.getWriteMethod().invoke(person, "lala");
        System.out.println(propertyDescriptor.getReadMethod().invoke(person));


        propertyDescriptor = BeanUtils.getPropertyDescriptor(Date.class, "time");
        System.out.println(propertyDescriptor.getReadMethod().invoke(new Date()));
    }


}
